package com.iwdnb.gkgz.ui.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.iwdnb.gkgz.application.service.StockPoolService;
import com.iwdnb.gkgz.common.model.dto.Stock;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockPredictionData;
import com.iwdnb.gkgz.common.utils.StockPredictionUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;
import com.iwdnb.job.annotation.Job;
import com.iwdnb.job.handler.BaseJobHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 股票验证昨日预测
 */
@Slf4j
@Job(cron = "0 50 14 * * ? ")
public class StockValidateYesterdayJob extends BaseJobHandler {

    @Autowired
    private StockPoolService stockPoolService;

    @Override
    public boolean doProcess(String parameter) {
        List<Stock> stockPoolDOS = stockPoolService.getStockList();
        if (CollectionUtils.isEmpty(stockPoolDOS)) {
            return true;
        }
        List<String> codes = stockPoolDOS.stream().map(Stock::getCode).collect(Collectors.toList());
        String year = DateUtil.format(new Date(), "yyyy");
        List<StockDayData> todayDatas = new ArrayList<>();

        for (String code : codes) {
            List<StockDayData> stockDayDataList = StockUtils.getHistoryDataByYear(code, year);
            stockDayDataList.add(StockUtils.getTodayStockData(code));
            stockDayDataList = StockPredictionUtils.predictionStock(stockDayDataList);
            todayDatas.add(stockDayDataList.get(stockDayDataList.size() - 1));

        }
        System.out.println("#################以下为今日验证结果,工作日下午2.50分以后有效##########################");
        for (StockDayData data : todayDatas) {
            log.info("{} {} 真实股价信息:最高价={},最低价={},开盘价={},收盘价={},今日预测信息:最高价={},最低价={},卖价匹配:{},买价匹配:{}", data.getDate(),
                data.getCode(), data.getMaxPrice(), data.getMinPrice(), data.getOpenPrice(), data.getClosePrice(),
                data.getTodayPrediction().getMaxPrice(), data.getTodayPrediction().getMinPrice(),
                data.getCheckInfo().isMatchSell(), data.getCheckInfo().isMatchBuy());
        }
        return true;
    }
}
